package com.example.blog.controller;

import com.example.blog.dto.AddCompanyReqDto;
import com.example.blog.dto.QueryCompanyResDto;
import com.example.blog.dto.QueryUserResDto;
import com.example.blog.dto.UpdateCompanyReqDto;
import com.example.blog.dto.base.IdRequestDto;
import com.example.blog.dto.base.PageIdReqDto;
import com.example.blog.entity.LiveCompany;
import com.example.blog.entity.LiveInfo;
import com.example.blog.response.CommonException;
import com.example.blog.response.PageableResponseModelDto;
import com.example.blog.response.ResponseModelDto;
import com.example.blog.response.ResponseModels;
import com.example.blog.service.LiveCompanyService;
import com.example.blog.service.LiveInfoService;
import com.example.blog.util.ParamUtil;
import com.example.blog.util.RequestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author Admin
 */
@RestController
@Api(value = "招聘信息接口", tags = "招聘信息接口")
@RequestMapping("/liveCompany")
public class LiveCompanyController {

    @Resource
    private LiveCompanyService companyService;

    @Resource
    private LiveInfoService liveInfoService;

    @GetMapping("/getCompany")
    @ApiOperation(value = "分页获取招聘信息")
    public PageableResponseModelDto<QueryCompanyResDto> getCompany(@Validated PageIdReqDto reqDto){
        return ResponseModels.page2ResponseModel(companyService.selectCompany(reqDto));
    }

    @PostMapping("/addCompany")
    @ApiOperation(value = "添加招聘信息接口")
    public ResponseModelDto addCompany(@RequestBody @Validated AddCompanyReqDto reqDto){
        String userId = RequestUtil.getUserId();
        LiveInfo liveInfo = liveInfoService.selectLiveInfoById(reqDto.getLiveInfoId());
        if(!Objects.equals(userId,liveInfo.getUserId())){
            throw new CommonException("只能给自己的直播间添加招聘信息");
        }
        if(companyService.addCompany(reqDto)){
            return ResponseModels.ok();
        }else{
            throw new CommonException("添加招聘信息失败");
        }
    }

    @PostMapping("/updateCompany")
    @ApiOperation(value = "修改招聘信息接口")
    public ResponseModelDto updateCompany(@RequestBody @Validated UpdateCompanyReqDto reqDto){
        String userId = RequestUtil.getUserId();
        LiveCompany liveCompany = companyService.selectCompanyById(reqDto.getId());
        if(ParamUtil.empty(liveCompany)){
            throw new CommonException("未查询到该招聘信息");
        }
        LiveInfo liveInfo = liveInfoService.selectLiveInfoById(liveCompany.getLiveInfoId());
        if(!Objects.equals(userId,liveInfo.getUserId())){
            throw new CommonException("只能给自己的直播间添加招聘信息");
        }
        if(companyService.updateCompany(reqDto)){
            return ResponseModels.ok();
        }else{
            throw new CommonException("修改招聘信息失败");
        }
    }

    @PostMapping("/changeCompanyStatus")
    @ApiOperation(value = "修改默认招聘信息接口")
    public ResponseModelDto changeCompanyStatus(@RequestBody @Validated IdRequestDto reqDto){
        String userId = RequestUtil.getUserId();
        LiveCompany liveCompany = companyService.selectCompanyById(reqDto.getId());
        if(ParamUtil.empty(liveCompany)){
            throw new CommonException("未查询到该招聘信息");
        }
        LiveInfo liveInfo = liveInfoService.selectLiveInfoById(liveCompany.getLiveInfoId());
        if(!Objects.equals(userId,liveInfo.getUserId())){
            throw new CommonException("只能给自己的直播间添加招聘信息");
        }
        if(companyService.changeCompanyDefault(reqDto)){
            return ResponseModels.ok();
        }else{
            throw new CommonException("修改默认失败");
        }
    }

    @PostMapping("/deleteCompany")
    @ApiOperation(value = "删除招聘信息接口")
    public ResponseModelDto deleteCompany(@RequestBody @Validated IdRequestDto reqDto){
        String userId = RequestUtil.getUserId();
        LiveCompany liveCompany = companyService.selectCompanyById(reqDto.getId());
        if(ParamUtil.empty(liveCompany)){
            throw new CommonException("未查询到该招聘信息");
        }
        LiveInfo liveInfo = liveInfoService.selectLiveInfoById(liveCompany.getLiveInfoId());
        if(!Objects.equals(userId,liveInfo.getUserId())){
            throw new CommonException("只能给自己的直播间添加招聘信息");
        }
        if(companyService.deleteCompany(reqDto)){
            return ResponseModels.ok();
        }else{
            throw new CommonException("删除招聘信息失败");
        }
    }



}
